<style lang="stylus" rel="stylesheet/stylus">

</style>

<template>
  <div class="category">
    <el-breadcrumb>
      <el-breadcrumb-item :to="'/'">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="main">
      <div class="header">
        <el-button type="primary" size="small" icon="plus" @click="addCategory">新增</el-button>
      </div>
      <div class="table">
        <el-table :data="categories">
          <el-table-column
            prop="name"
            label="名称">
          </el-table-column>
          <el-table-column
            prop="articleCount"
            label="文章数">
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间">
          </el-table-column>
          <el-table-column label="操作">
            <template scope="scope">
              <el-button
                size="small"
                type="text"
                @click="editCategory(scope.row.id,scope.row.name)">编辑
              </el-button>
              <el-button
                size="small"
                type="text"
                @click="deleteCategory(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page">
        <el-pagination
          @current-change="currentChange"
          :current-page="pageNumber"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
        </el-pagination>
      </div>
      <el-dialog title="分类" :visible.sync="dialogVisible" size="tiny" :modal-append-to-body="false">
        <el-form :model="form">
          <input type="hidden" v-model="form.id">
          <el-form-item label="分类名称" label-width="100px">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveCategory">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import util from '../../assets/js/util'
  export default{
    data(){
      return {
        pageNumber: 1,
        pageSize: 15,
        total: 0,
        categories: [],
        dialogVisible: false,
        form: {
          id: '',
          name: ''
        }
      }
    },
    methods: {
      init(){
        util.api
          .post('/api/admin/categories', {pageNumber: this.pageNumber, pageSize: this.pageSize})
          .then(data => {
            if (data.isOk) {
              this.total = data.total
              this.categories = data.categories
            } else {
              this.$message.error(data.errmsg)
            }
          })
      },
      currentChange(val){
        this.pageNumber = val;
        this.init()
      },
      addCategory(){
        this.form.id = ''
        this.form.name = ''
        this.dialogVisible = true
      },
      editCategory(id, name){
        this.form.id=id
        this.form.name=name
        this.dialogVisible = true
      },
      deleteCategory(id){
          util.api.post('/api/admin/deleteCategory',{categoryId:id})
            .then(data=>{
                if (data.isOk){
                    this.init()
                }else{
                  this.$message.error(data.errmsg)
                }
            })
      },
      saveCategory(){
        if (this.form.name){
          util.api.post('/api/admin/saveCategory',this.form)
            .then(data=>{
                if (data.isOk){
                    this.init()
                }else{
                  this.$message.error(data.errmsg)
                }
              this.dialogVisible = false
            })
        }else{
          this.$message.error('请输入分类名称')
        }
      }
    },
    created(){
      this.init()
    }
  }
</script>
